package top.kaoshanji.ch07;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * 代码清单 ch07-01：分析堆栈轨迹元素(7.2.6节)
 * author kaoshanji
 * ^_^
 * create time 2024/11/27 21:32
 */
public class StackTraceTest {

    private static final Logger logger = LogManager.getLogger(StackTraceTest.class);

    public static void main(String[] args) {
        logger.info("...代码清单 ch07-01：分析堆栈轨迹元素(7.2.6节)...");

        factorial(3);
    }


    public static int factorial(int n) {
        System.out.println("factorial(" + n + "):");
        var walker = StackWalker.getInstance();
        walker.forEach(System.out::println);

        int r;
        if (n <= 1) r = 1;
        else r = n * factorial(n - 1);
        System.out.println("return " + r);
        return r;
    }

}
